﻿using DetectingKinectSencor._40_Tools;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard
{
    class MultimediaViewModelDashboard : Notifier
    {
        private DateTime lastElementCaptureTime;

        public MultimediaViewModelDashboard(DateTime lastElementCaptureTime)
        {
            this.lastElementCaptureTime = lastElementCaptureTime;
            tempPlotModel = new PlotModel();
            SetUpModel();
            LoadData(lastElementCaptureTime);
        }

        // Internal variable in which the model is saved in this class
        private PlotModel plotModel;
        public PlotModel tempPlotModel;
        private LineSeries timepointSerieVideoDetectedList;
        private LineSeries timepointSerieImageDetectedList;

        // Important the UI needs to bind the Model to SlideChangePlotModel!!!
        public PlotModel MultimediaPlotModel
        {
            get { return plotModel; }
            set { plotModel = value; RaisePropertyChanged(() => MultimediaPlotModel); }
        }

        private void SetUpModel()
        {
            tempPlotModel.LegendTitle = "Legend";
            tempPlotModel.LegendOrientation = LegendOrientation.Horizontal;
            tempPlotModel.LegendPlacement = LegendPlacement.Outside;
            tempPlotModel.LegendPosition = LegendPosition.TopRight;
            tempPlotModel.LegendBackground = OxyColor.FromAColor(200, OxyColors.White);
            tempPlotModel.LegendBorder = OxyColors.Black;
            tempPlotModel.LegendFontSize = DesignOfGraphs.fontSizeDashboard;


            // Axes which shows the date when the speaker looked away
            TimeSpanAxis timeLineAxis = new TimeSpanAxis(AxisPosition.Bottom, "Timeline (mm:ss)", "mm:ss")
                                                        {
                                                            MajorGridlineStyle = LineStyle.Solid,
                                                            MinorGridlineStyle = LineStyle.Dot,
                                                            FontSize = DesignOfGraphs.fontSizeDashboard
                                                        };

            // Add the axes to the plot model
            tempPlotModel.Axes.Add(timeLineAxis);

            // Axes which shows the Amount of time the speaker stood on the same spot
            TimeSpanAxis timeSpanAxis = new TimeSpanAxis(AxisPosition.Left, "Length (mm:ss)", "mm:ss")
                                                        {
                                                            MajorGridlineStyle = LineStyle.Solid,
                                                            MinorGridlineStyle = LineStyle.Dot,
                                                            FontSize = DesignOfGraphs.fontSizeDashboard
                                                        };

            // Add the axes to the plot model
            tempPlotModel.Axes.Add(timeSpanAxis);

        }

        public void LoadData(DateTime captureTime)
        {
            // Get the list with the viewingDirectionData
            VideoDetectedData videoDetectedList = EvaluationData.videoDetectedData;
            ImageDetectedData imageDetectedList = EvaluationData.imageDetectedData;
            TimeSpan elapsedTime;

            timepointSerieVideoDetectedList = new LineSeries
            {
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = Graph.colors[1],
                MarkerType = Graph.markerTypes[1],
                CanTrackerInterpolatePoints = false,
                Title = string.Format("Videos"),
                Smooth = false,
            };


            timepointSerieImageDetectedList = new LineSeries
            {
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = Graph.colors[2],
                MarkerType = Graph.markerTypes[2],
                CanTrackerInterpolatePoints = false,
                Title = string.Format("Images"),
                Smooth = false,
            };



            foreach (CaptureTime videoDetected in videoDetectedList.trackingSequences)
            {
                if (captureTime == videoDetected.captureTime)
                {
                    for (int i = 0; i < videoDetected.timePoints.Count; i++)
                    {
                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = videoDetected.timePoints[i].timePoint - captureTime;
                        timepointSerieVideoDetectedList.Points.Add(new DataPoint(TimeSpanAxis.ToDouble(elapsedTime),
                                                                          TimeSpanAxis.ToDouble(videoDetected.timePoints[i].timeSpan)));
                    }

                    tempPlotModel.Series.Add(timepointSerieVideoDetectedList);
                    break;
                }
            }

            foreach (CaptureTime imageDetected in imageDetectedList.trackingSequences)
            {
                if (captureTime == imageDetected.captureTime)
                {
                    for (int i = 0; i < imageDetected.timePoints.Count; i++)
                    {
                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = imageDetected.timePoints[i].timePoint - captureTime;

                        timepointSerieImageDetectedList.Points.Add(new DataPoint(
                                                                      TimeSpanAxis.ToDouble(elapsedTime),
                                                                      TimeSpanAxis.ToDouble(new TimeSpan(0,0,1))));
                    }

                    // Add the time point to the date/time graph
                    tempPlotModel.Series.Add(timepointSerieImageDetectedList);

                    break;
                }
            }

            //Saved the tempPlotModel to the plotModel than is the OnPropertyChanged executed and the UI is updated
            MultimediaPlotModel = tempPlotModel;
        }
    }
}
